//
//  BCWalletCoinView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/15.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCWalletCoinView.h"

static CGFloat BCWalletCoinViewLabelPadding = 2.0f;

@interface BCWalletCoinView ()

@property (nonatomic, strong) UILabel *btcLabel;

@property (nonatomic, strong) UILabel *ethLabel;

@property (nonatomic, strong) UILabel *btcMoneyLabel;

@property (nonatomic, strong) UILabel *ethMoneyLabel;

/**
 *sepLine
 */
@property (nonatomic, strong) UIView *sepLine;

@end

@implementation BCWalletCoinView

+ (UIFont *)coinFont {
    return [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
}
+ (CGFloat)coinHeight {
    return [self coinFont].lineHeight+2;
}
+ (UIFont *)moneyFont {
    return [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
}
+ (CGFloat)moneyHeight {
    return [self moneyFont].lineHeight+2;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.btcLabel = [UILabel new];
    self.btcLabel.adjustsFontSizeToFitWidth = true;
    self.btcLabel.textAlignment = NSTextAlignmentLeft;
    self.btcLabel.font = [self.class coinFont];
    [self addSubview:self.btcLabel];
    [self.btcLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20.0);
        make.right.mas_equalTo(self.mas_centerX).offset(-10.0);
        make.height.mas_equalTo([self.class coinHeight]);
    }];
    //
    self.ethLabel = [UILabel new];
    self.ethLabel.adjustsFontSizeToFitWidth = true;
    self.ethLabel.textAlignment = NSTextAlignmentRight;
    self.ethLabel.font = [self.class coinFont];
    [self addSubview:self.ethLabel];
    [self.ethLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-20.0);
        make.left.mas_equalTo(self.mas_centerX).offset(10.0);
        make.height.mas_equalTo([self.class coinHeight]);
    }];
    //
    CGFloat padding = BCWalletCoinViewLabelPadding;
    self.btcMoneyLabel = [UILabel new];
    self.btcMoneyLabel.adjustsFontSizeToFitWidth = true;
    self.btcMoneyLabel.textAlignment = self.btcLabel.textAlignment;
    self.btcMoneyLabel.font = [self.class moneyFont];
    [self addSubview:self.btcMoneyLabel];
    [self.btcMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.btcLabel.mas_bottom).offset(padding);
        make.left.mas_equalTo(self.btcLabel);
        make.right.mas_equalTo(self.btcLabel);
        make.height.mas_equalTo([self.class moneyHeight]);
    }];
    //
    self.ethMoneyLabel = [UILabel new];
    self.ethMoneyLabel.adjustsFontSizeToFitWidth = true;
    self.ethMoneyLabel.textAlignment = self.ethLabel.textAlignment;
    self.ethMoneyLabel.font = [self.class moneyFont];
    [self addSubview:self.ethMoneyLabel];
    [self.ethMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.ethLabel.mas_bottom).offset(padding);
        make.left.mas_equalTo(self.ethLabel);
        make.right.mas_equalTo(self.ethLabel);
        make.height.mas_equalTo([self.class moneyHeight]);
    }];
    
    //
    self.sepLine = [UIView new];
    [self addSubview:self.sepLine];
    [self.sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1.0);
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.ethLabel.mas_centerY);
        make.bottom.mas_equalTo(self.ethMoneyLabel.mas_centerY);
    }];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    //
    UIColor *textColor = [UIColor lightTextColor];
    if (![backgroundColor qmui_colorIsDark]) {
        textColor = [UIColor darkTextColor];
    }
    self.btcLabel.textColor = textColor;
    self.ethLabel.textColor = textColor;
    self.btcMoneyLabel.textColor = textColor;
    self.ethMoneyLabel.textColor = textColor;
    self.sepLine.backgroundColor = textColor;
}

- (void)setEthValue:(NSString *)ethValue {
    _ethValue = ethValue;
    //
    self.ethLabel.attributedText = [self coinValueWithCoinString:kETH value:ethValue];
}
- (void)setEthMoneyValue:(NSString *)ethMoneyValue {
    _ethMoneyValue = ethMoneyValue;
}
//
- (void)setBtcValue:(NSString *)btcValue {
    _btcValue = btcValue;
    //
    self.btcLabel.attributedText = [self coinValueWithCoinString:kBTC value:btcValue];
}
- (void)setBtcMoneyValue:(NSString *)btcMoneyValue {
    _btcMoneyValue = btcMoneyValue;
}


//
- (void)setBtcValue:(NSString *)btcValue moneyValue:(NSString *)moneyValue monetary:(NSString *)monetary{
    self.btcValue = btcValue;
    //money
    NSString *money = [NSString stringWithFormat:@"≈%@ %@",moneyValue,monetary];
    self.btcMoneyLabel.text = money;
}
- (void)setEthValue:(NSString *)ethValue moneyValue:(NSString *)moneyValue monetary:(NSString *)monetary{
    self.ethValue = ethValue;
    //money
    NSString *money = [NSString stringWithFormat:@"≈%@ %@",moneyValue,monetary];
    self.ethMoneyLabel.text = money;
    
}


#pragma mark -
//设置btc或者eth
- (NSAttributedString *)coinValueWithCoinString:(NSString *)coinString value:(NSString *)value {
    coinString = coinString?:@"";
    value = value?:@"--";
    //
    NSString *totle = [NSString stringWithFormat:@"%@  %@",coinString,value];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:totle];
    NSRange coinRange = [totle rangeOfString:coinString];
    if (coinRange.location!=NSNotFound) {
        [attri addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular]} range:coinRange];
    }
    return attri;
}



+ (CGFloat)defaultHeight {
    CGFloat height = 0;
    height += [self coinHeight];
    height += [self moneyHeight];
    height += 26.0;
    return height;
}

@end
